#pragma once

#include "ColligereDatabaseInterfaceBase.hpp"
#include "ColligereLogDialog.hpp"
#include "ColligereDataImporterBase.hpp"

namespace col {

/*! @brief The main frame class.
 *
 * Handles creating of the frame layout, passing top-level events 
 * (like menu events) to the active database interface etc.
 **/
class MainFrame : public wxFrame
{
public:
	MainFrame();
	virtual ~MainFrame();

	bool CloseDatabase();
	bool CreateDatabase();
	bool OpenDatabase ( const wxString &rFileName = wxString() );
	bool IsDatabaseOpened() const;

	MainFrame &EnableAdding ( bool enable = true );
	MainFrame &EnableCopy ( bool enable = true );
	MainFrame &EnablePaste ( bool enable = true );
	MainFrame &EnableClearInputs ( bool enable = true );
	MainFrame &EnableDeselectItem ( bool enable = true );
	MainFrame &EnableDelete ( bool enable = true );
	MainFrame &EnableUpdate ( bool enable = true );

	void EnableFileOperations ( bool enable = true );
	void EnableItemOperations ( bool enable = true );

	bool Exit();
private:
	const wxRect DetermineFrameSize() const;

	void CreateToolBars();
	void CreateMenuBar();

	void ConnectGuiEventHandlers();
	void ConnectDatabaseEventHandlers();

	void LoadLayout();
	void SaveLayout();

	wxAuiToolBar *GetFileOperationsToolBar();
	wxAuiToolBar *GetItemOperationsToolBar();

	wxMenuItem *CreateMenuItem ( wxMenu *pParent, wxInt32 id, 
		const wxString &rLabel, const wxString &rDescription = empty_string, 
		wxUint32 iconIndex = 0, wxItemKind kind = wxITEM_NORMAL );

	void CreateToolBarItem ( wxAuiToolBar *pParent, wxInt32 id, 
		const wxString &rLabel, const wxString &rDescription, 
		wxUint32 iconIndex );

	/////////////////////////////////////////
	// Event handlers
	/////////////////////////////////////////

	void OnMenuNew ( wxCommandEvent &rEvent );
	void OnMenuOpen ( wxCommandEvent &rEvent );
	void OnMenuExit ( wxCommandEvent &rEvent );
	void OnMenuAbout ( wxCommandEvent &rEvent );
	void OnMenuClose ( wxCommandEvent &rEvent );
	void OnMenuViewLog ( wxCommandEvent &rEvent );
	void OnMenuSaveLayout ( wxCommandEvent &rEvent );
	void OnMenuLoadLayout ( wxCommandEvent &rEvent );
	void OnMenuPreferences ( wxCommandEvent &rEvent );
	void OnMenuCopy ( wxCommandEvent &rEvent );
	void OnMenuOpenRecent ( wxCommandEvent &rEvent );
	void OnMenuSearch ( wxCommandEvent &rEvent );
	void OnMenuSaveAs ( wxCommandEvent &rEvent );
	void OnMenuProperties ( wxCommandEvent &rEvent );

	void OnMenu ( wxCommandEvent &rEvent );
	void OnClose ( wxCloseEvent &rEvent );

	void OnDatabaseDeleteEvent ( DatabaseEventBase &rEvt );
	void OnDatabaseRestoreEvent ( DatabaseEventBase &rEvt );
	void OnDatabaseAddEvent ( DatabaseEventBase &rEvt );

	void OnImportStart ( DataImporterEvent &rEvt );
	void OnImportEnd ( DataImporterEvent &rEvt );

	/////////////////////////////////////////
	// Member variables
	/////////////////////////////////////////

	typedef boost::shared_ptr < DatabaseInterfaceBase > DatabaseInterfaceBasePtr;
	//! The currently opened database.
	DatabaseInterfaceBasePtr mCurDb;

	//! A log dialog for showing log events in the HTML-format.
	LogDialog *mpLogDlg;
	//! The wxAUI frame manager for this frame.
	wxAuiManager mFrameMngr;

	wxFileHistory mFileHistory;

	wxAuiToolBar *mpFileOprTb, *mpItemOprTb;

// public:
	DECLARE_DYNAMIC_CLASS ( MainFrame );
};

//! Database drop target class.
class DatabaseDropTarget : public wxFileDropTarget
{
public:
	DatabaseDropTarget ( MainFrame *pParent );
	bool OnDropFiles ( wxCoord, wxCoord, const wxArrayString &rFileNames );
private:
	MainFrame *mpParent;
};

}